(function() {

/**
 * Visual module that contains terminals. The wires are updated when the module is dragged around.
 * @class WebUIFormContainer
 * @namespace WireIt
 * @constructor
 * @param {Object}   options      Configuration object (see options property)
 * @param {WireIt.Layer}   layer The WireIt.Layer (or subclass) instance that contains this container
 * @author Igor Lazarev
 */
WireIt.WebUIFormContainer = function(options, layer) {

WireIt.WebUIFormContainer.superclass.constructor.call(this, options, layer);

}

YAHOO.extend(WireIt.WebUIFormContainer, WireIt.WebUIContainer, {
    
setOptions: function(options) {
	
	WireIt.WebUIFormContainer.superclass.setOptions.call(this,options);
	
	  this.options.legend = options.legend;
      this.options.collapsible = options.collapsible; 
      this.options.fields = options.fields;
	
},

render: function(){
	WireIt.WebUIFormContainer.superclass.render.call(this);
    this.renderForm();	
},

renderForm: function() {
      //alert("renderForm");
	  for(var i = 0 ; i < this.options.fields.length ; i++) {
         this.options.fields[i].inputParams.container = this;
      }
      var groupParams = {parentEl: this.bodyEl, fields: this.options.fields, legend: this.options.legend, collapsible: this.options.collapsible};
	  this.form = new YAHOO.inputEx.Group(groupParams); //the form is created here
      YAHOO.util.Event.addListener(this.bodyEl.childNodes[0].childNodes[0].childNodes[0], "change", function(){//listen to the form
		this.onFormChange();							
		}, this, true);
	
	var propertiesPanel = this.bodyEl.getElementsByClassName("inputEx-TypeField-PropertiesPanel")[0];
	var pencil = this.bodyEl.getElementsByClassName("inputEx-TypeField-EditButton")[0];
		YAHOO.util.Event.addListener(pencil,"click", function(){//listen to the pencil clicks
			if (JSON.stringify(propertiesPanel.style)!="{}") {
				//alert("hidden");
				this.onFormIdChange();
			}
												
		}, this, true);
},

getValue: function() {
  return this.form.getValue();
},

setValue: function(val) {
  this.form.setValue(val);
},

getConfig: function() {
	 var obj = WireIt.WebUIFormContainer.superclass.getConfig.call(this);
     return obj;	
},

onAddWire: function(event, args){
		WireIt.WebUIFormContainer.superclass.onAddWire.call(this,event, args);
		this.initState();
},
	
onRemoveWire: function(event, args){
		WireIt.WebUIFormContainer.superclass.onRemoveWire.call(this,event, args);
		this.initState();
},

onMouseDown: function() { //if the container is selected

  if(this.layer) {
			
	 if(this.layer.focusedContainer && this.layer.focusedContainer != this) {
        this.layer.focusedContainer.removeFocus();
	}
	
	 this.setFocus();
     this.layer.focusedContainer = this;
	 this.layer.focusContainer(this); //fire a 'container focused' event

  }

},

onFormChange: function(){
//	this.initType();
	this.initState();
},

onFormIdChange: function(){
	this.initType();
	this.initState();	
},

positionTerminals: function(){
	//override me
},

removeFocus: function(){
	WireIt.WebUIFormContainer.superclass.removeFocus.call(this);
},

initType: function () {
	//override me
},   
  
initState: function() {
	//override me
}		
   
});

})()